
thresh = 0.08;
clear alerts realbal;
show = 0;

realbal = localBallance('/Users/justaceclutter/Downloads/accountballance.xls');

figure;
stairs(realbal(:,1),realbal(:,2),'-r');
datetick;
xlabel('Date');
ylabel('US Dollars');
title('Real Checking Account Ballence', 'FontWeight', 'bold');

models = getMoneyInputs();

res = cell(0);
dres = cell(0);
am = 400:25:800;

b = 8;
l = 10;
d = 20;

am = b + l + d;
am = [25 600/26 40];
amgraphics = {'-r', '-g', '-r'};
amnames = {'$25 / Day Food', sprintf('$%2.0f / Day Food', 600/26), '$40 / Day Food'};

%am = 600 / 26;

for i = 1:length(am)
    minputs = getMoneyInputs(am(i),0,0);
    [tmp tmpalerts]    = TripBallence(minputs, thresh);

    tempres = zeros(size(tmp));
    for j = 1:size(tmp,1)
        sday = tmp(j,1);
        
        indexs = find(realbal(:,1) <= sday);
        rbal = realbal(indexs(end),2);        
        dbal = rbal - tmp(j,2);
        tempres(j,1) = sday;
        if realbal(end,1) < sday
            dbal = NaN;
        end
        
        tempres(j,2) = dbal;
    end
    
    res{i}    = tmp;
    alerts{i} = tmpalerts;
    dres{i}   = tempres;
end

starttime = 999999;
stoptime = res{1}(end,1);

for i = 1:size(res{1}, 1)
    if res{1}(i,2) ~= 0
        if res{1}(i,1) < starttime
            starttime = res{1}(i,1);
        end
    end        
end

limits = [999999,0];

fig = figure;
%set(fig, 'PaperOrientation', 'Landscape');
if show == 1
    set(fig, 'OuterPosition', [0 0 800 450]);
    axes1 = axes('Position', [0.1, 0.23, 0.4, 0.7]);
    axes2 = axes('Position', [0.1, 0.1, 0.4, 0.1]);
else
    axes1 = axes('Position', [0.12, 0.23, 0.83, 0.7]);
    axes2 = axes('Position', [0.12, 0.1, 0.83, 0.1]);
end

axes(axes1);
clear showbal;
showbal = zeros(0,2);
for i = 1:size(realbal,1)
    if realbal(i,1) >= starttime && realbal(i,1) <= stoptime
        showbal = [showbal; realbal(i,1), realbal(i,2)];
    end
end
tmax = max(showbal(:,2));
tmin = min(showbal(:,2));
limits(1) = min(limits(1), tmin);
limits(2) = max(limits(2), tmax);

clear minvals maxvals;
minvals = zeros(stoptime - starttime,2) + 9999999;
maxvals = zeros(stoptime - starttime,2);
for i = 1:stoptime - starttime
    minvals(i, 1) = starttime + i - 1;
    maxvals(i, 1) = starttime + i - 1;
end

for m = 1:size(showbal,1)
    dat = showbal(m,1);
    [a b] = sort(abs(minvals(:,1)-dat));
    index = b(1);
    if minvals(index,2) > showbal(m,2)
        minvals(index,2) = showbal(m,2);
    end
    if maxvals(index,2) < showbal(m,2)
        maxvals(index,2) = showbal(m,2);
    end
end


stairs(showbal(:,1), showbal(:,2), '-b', 'LineWidth', 3);
hold on;

for i = 1:length(am)
    showbal = zeros(0,2);
    for j = 1:size(res{i},1)
        if res{i}(j,1) >= starttime && res{i}(j,1) <= stoptime
            showbal = [showbal; res{i}(j,1), res{i}(j,2)];
        end
    end
    
    for m = 1:size(showbal,1)
        dat = showbal(m,1);
        [a b] = sort(abs(minvals(:,1)-dat));
        index = b(1);
        if minvals(index,2) > showbal(m,2)
            minvals(index,2) = showbal(m,2);
        end
        if maxvals(index,2) < showbal(m,2)
            maxvals(index,2) = showbal(m,2);
        end
    end
    
    tmax = max(showbal(:,2));
    tmin = min(showbal(:,2));
    limits(1) = min(limits(1), tmin);
    limits(2) = max(limits(2), tmax);
    gtype = amgraphics{i};
    stairs(showbal(:,1), showbal(:,2),gtype);    
    hold on;
end

limitw = limits(2)-limits(1);
limitm = 0.5*(limits(1) + limits(2));
limits(1) = limitm - 0.5*(limitw/0.8);
limits(2) = limitm + 0.5*(limitw/0.8);

set(gca, 'XTick', []);
set(gca, 'XLim', [starttime stoptime]);
set(gca, 'YLim', limits);
ylabel('Bank Ballance (US Dollars)');
title('Projected Checking Account Ballance', 'FontWeight', 'bold');


if show == 1
    five = diff(get(gca, 'YLim'))*0.05;

    showindex = 1;
    thealerts = alerts{showindex};
    for j = 2:size(thealerts,1)
        fprintf('showing an alert\n');
        tday = thealerts{j,2};
        [a b] = sort(abs(maxvals(:,1)-tday));
        yval = maxvals(b(1),2) + five;
        xr = diff(get(gca, 'XLim'))*0.03;
        yr = diff(get(gca, 'YLim'))*0.03;
        xs = diff(get(gca, 'XLim'));
        ys = diff(get(gca, 'YLim'));
        annotation('ellipse', [tday/xs yval/ys 0.05 0.05])
        [x y] = ellipsoid(tday, yval, 0, xr,yr,0);
%        plot(x,y,'b');
        text(tday, yval, sprintf('%i', thealerts{j,1}), 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Middle');
    end
end

axes(axes2);
limits = [999999 0];
for i = 1:length(am)
    showbal = zeros(0,2);
    for j = 1:size(dres{i},1)
        if dres{i}(j,1) >= starttime && dres{i}(j,1) <= stoptime
            showbal = [showbal; dres{i}(j,1), dres{i}(j,2)];
        end
    end
    
    tmax = max(showbal(:,2));
    tmin = min(showbal(:,2));
    limits(1) = min(limits(1), tmin);
    limits(2) = max(limits(2), tmax);
    gtype = amgraphics{i};
    stairs(showbal(:,1), showbal(:,2),gtype);
    hold on;
end

limitw = limits(2)-limits(1);
limitm = 0.5*(limits(1) + limits(2));
limits(1) = limitm - 0.5*(limitw/0.8);
limits(2) = limitm + 0.5*(limitw/0.8);


set(gca, 'XLim', [starttime stoptime]);
set(gca, 'YLim', limits);
set(gca, 'XTick', starttime:2:stoptime);
datetick('x',6, 'keeplimits', 'keepticks');
xlabel('Date');
ylabel('Differnece');

fp = fillPage(gcf, 'margins', [0 0 0 0], 'papersize', [11 8.5]);
print -painters -dpdf -r600 '/Users/justaceclutter/Desktop/TripProjection.pdf';
set(gcf, fp);
